package com.weiyuwqj.admin.controller.admin.sys;


import com.weiyuwqj.api.service.SysNoticeService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.SysNotice;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.ExcelUtils;
import com.weiyuwqj.api.util.IpUtils;
import com.weiyuwqj.api.util.Sms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/1.
 */
@Controller
@RequestMapping("/sys/smsMessage")
public class SysSmsMessageController {
    @Autowired
    SysNoticeService sysNoticeService;
    @Autowired
    BasUserService userService;

    @ResponseBody
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Object send(String mobileString, String content, String title, String sendDateTime) {
        SysNotice sysNotice = new SysNotice();
        mobileString = mobileString.replaceAll(" ", "");
        String[] phoneArray = mobileString.split(",");
        Long time = null;
        if (sendDateTime != null) {
            time = DateUtils.parseDate(sendDateTime).getTime();
        }
        for (int i = 0; i < phoneArray.length; i++) {
            BasUser user = userService.getUserByMobile(phoneArray[i]);
            Object response = Sms.send(phoneArray[i], content);
            if (user != null) {
                sysNotice.setTypee(3);
                sysNotice.setStatus(Long.parseLong(response.toString()) > 0 ? 1 : 0);
                sysNotice.setContents(content);
                sysNotice.setReceiveId(user.getMobile());
                sysNotice.setTitle(title);
                sysNotice.setUserId(user.getId());
                sysNotice.setAddip(IpUtils.get());
                sysNoticeService.save(sysNotice);
            } else {
                sysNotice.setTypee(3);
                sysNotice.setStatus(Long.parseLong(response.toString()) > 0 ? 1 : 0);
                sysNotice.setContents(content);
                sysNotice.setReceiveId("未注册用户" + phoneArray[i]);
                sysNotice.setTitle(title);
                sysNotice.setUserId(0);
                sysNotice.setAddip(IpUtils.get());
                sysNoticeService.save(sysNotice);
            }

        }
        return Message.OK;
    }


    //获取上传用户列表
    @ResponseBody
    @RequestMapping(value = "/listUploadUser", method = RequestMethod.POST)
    public Object listUploadUser(MultipartFile file) {
        List<List> list = null;
        if (file == null) {
            return list;
        } else {
            InputStream is = null;
            try {
                is = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path = file.getOriginalFilename();
            list = ExcelUtils.readExcel(is, path, 1);

        }
        String retMobile = "";
        if (list == null) {
            return retMobile;
        }
        for (List innerList : list) {
            for (Object mobile : innerList) {
                retMobile = retMobile + mobile + ",";
            }
        }
        Map map = new HashMap();
        map.put("mobile", retMobile);
        return map;
    }
}